// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Core.Search;

internal sealed partial class SourceFilterConverter : JsonConverter<SourceFilter>
{
	public override SourceFilter Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		var variant = new SourceFilter();
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				var property = reader.GetString();
				if (property == "excludes" || property == "exclude")
				{
					reader.Read();
					variant.Excludes = new FieldsConverter().Read(ref reader, typeToConvert, options);
					continue;
				}

				if (property == "includes" || property == "include")
				{
					reader.Read();
					variant.Includes = new FieldsConverter().Read(ref reader, typeToConvert, options);
					continue;
				}
			}
		}

		return variant;
	}

	public override void Write(Utf8JsonWriter writer, SourceFilter value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.Excludes is not null)
		{
			writer.WritePropertyName("excludes");
			new FieldsConverter().Write(writer, value.Excludes, options);
		}

		if (value.Includes is not null)
		{
			writer.WritePropertyName("includes");
			new FieldsConverter().Write(writer, value.Includes, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(SourceFilterConverter))]
public sealed partial class SourceFilter
{
	public Elastic.Clients.Elasticsearch.Serverless.Fields? Excludes { get; set; }
	public Elastic.Clients.Elasticsearch.Serverless.Fields? Includes { get; set; }
}

public sealed partial class SourceFilterDescriptor<TDocument> : SerializableDescriptor<SourceFilterDescriptor<TDocument>>
{
	internal SourceFilterDescriptor(Action<SourceFilterDescriptor<TDocument>> configure) => configure.Invoke(this);

	public SourceFilterDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Fields? ExcludesValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Fields? IncludesValue { get; set; }

	public SourceFilterDescriptor<TDocument> Excludes(Elastic.Clients.Elasticsearch.Serverless.Fields? excludes)
	{
		ExcludesValue = excludes;
		return Self;
	}

	public SourceFilterDescriptor<TDocument> Includes(Elastic.Clients.Elasticsearch.Serverless.Fields? includes)
	{
		IncludesValue = includes;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (ExcludesValue is not null)
		{
			writer.WritePropertyName("excludes");
			JsonSerializer.Serialize(writer, ExcludesValue, options);
		}

		if (IncludesValue is not null)
		{
			writer.WritePropertyName("includes");
			JsonSerializer.Serialize(writer, IncludesValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class SourceFilterDescriptor : SerializableDescriptor<SourceFilterDescriptor>
{
	internal SourceFilterDescriptor(Action<SourceFilterDescriptor> configure) => configure.Invoke(this);

	public SourceFilterDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Fields? ExcludesValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Fields? IncludesValue { get; set; }

	public SourceFilterDescriptor Excludes(Elastic.Clients.Elasticsearch.Serverless.Fields? excludes)
	{
		ExcludesValue = excludes;
		return Self;
	}

	public SourceFilterDescriptor Includes(Elastic.Clients.Elasticsearch.Serverless.Fields? includes)
	{
		IncludesValue = includes;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (ExcludesValue is not null)
		{
			writer.WritePropertyName("excludes");
			JsonSerializer.Serialize(writer, ExcludesValue, options);
		}

		if (IncludesValue is not null)
		{
			writer.WritePropertyName("includes");
			JsonSerializer.Serialize(writer, IncludesValue, options);
		}

		writer.WriteEndObject();
	}
}